package com.xc.hotelmgr.datapusherali.service.fz.impl;

import com.xc.hotelmgr.datapusherali.pojo.mq.RpCallBack;
import com.xc.hotelmgr.datapusherali.pojo.mybatis.*;
import com.xc.hotelmgr.datapusherali.service.fz.RateplanService;
import com.xc.hotelmgr.datapusherali.service.mybatis.*;
import com.xc.hotelmgr.datapusherali.utils.FZUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class RateplanServiceImpl implements RateplanService {

    @Autowired
    private OpHtlGroupService opHtlGroupService;

    @Autowired
    private HtLogicRoomService htLogicRoomService;

    @Autowired
    private DtUpHtlService dtUpHtlService;

    @Autowired
    private DtStoreService dtStoreService;

    @Autowired
    private OpTaskDetailService opTaskDetailService;

    @Override
    public void rpCallbackUpdate(RpCallBack rpCallBack) {
        Long rpId = rpCallBack.getRpId();
        // 查询酒店编码
        HtLogicRoom htLogicRoom = htLogicRoomService.getById(rpId);
        if(htLogicRoom==null) {
            return;
        }
        Long hotelCode = htLogicRoom.getHotelCode();
        // 查询酒店是否已经推送到店铺
        int storeId = rpCallBack.getStoreId();

        DtUpHtl dtUpHtl = dtUpHtlService.getByStoreIdAndHotelId(storeId, hotelCode);

        if(dtUpHtl!=null) {
            // 数据库关联查询店铺自定义价格政策
            OpHtlGroup opHtlGroup = opHtlGroupService.getRatePlan(storeId, hotelCode);
            // 默认早餐，退订政策
            if (opHtlGroup == null) { // 如果没有自定义价格政策则进行推送， 反之不推送
                String appKey = rpCallBack.getAppKey();
                String sessionKey = rpCallBack.getSessionKey();
                String secret = rpCallBack.getSecret();
                String result = FZUtil.logicRoomUpdate(htLogicRoom, appKey, sessionKey, secret);
                if (!result.contains("xhotel_rateplan_update_response")) {
                    OpTaskDetail opTaskDetail = new OpTaskDetail();
                    opTaskDetail.setHotelId(hotelCode);
                    opTaskDetail.setReason(rpId + "&" +"价格政策推送失败" + "&" + result);
                    opTaskDetailService.insertIgnoreNull(opTaskDetail);
                }
            }
        }
    }

    @Override
    public boolean rpDel(Long rpId, int opOurRateplanId, String appKey, String sessionKey, String secret) {
//        HtLogicRoom htLogicRoom = htLogicRoomService.getById(rpId);
        HtLogicRoom htLogicRoom =new HtLogicRoom();
        String newRpCode = String.valueOf(rpId);
        if(opOurRateplanId !=0) {
            newRpCode = rpId + "XC" + opOurRateplanId;
        }
        htLogicRoom.setRateplan_code(newRpCode);
        String result = FZUtil.logicRoomDel(htLogicRoom, appKey, sessionKey, secret);
        return result.contains("xhotel_rateplan_delete_response");
    }


    /**
     * 删除价格政策（自定义价格政策）
     * @param groupId
     * @param hotelId
     * @param storeId
     * @return
     */
    @Override
    public boolean delGroupRp(Integer groupId, Long hotelId, Integer storeId) {
        DtStore dtStore = dtStoreService.getById(storeId);
        if(dtStore!=null) {
            String appKey = dtStore.getAppkey();
            String sessionKey = dtStore.getSessionkey();
            String secret = dtStore.getSecret();
            OpHtlGroup opHtlGroup = opHtlGroupService.getDelRatePlan(storeId, hotelId,groupId);
            log.info("=====" + groupId + "=====");
            List<OpOurRateplan> opOurRateplans = opHtlGroup.getOpOurRateplan();
            List<HtLogicRoom> htLogicRooms = htLogicRoomService.selectByHotelCode(hotelId);
            for (HtLogicRoom htLogicRoom : htLogicRooms) {
                Long rpCode = htLogicRoom.getRpCode();
                for (OpOurRateplan opOurRateplan : opOurRateplans) {
                    int id = opOurRateplan.getId();
                    String newRpCode = rpCode + "XC" + id;
                    htLogicRoom.setRateplan_code(newRpCode);
                    log.info(newRpCode);
                    String result = FZUtil.logicRoomDel(htLogicRoom, appKey, sessionKey, secret);
                    if(!result.contains("xhotel_rateplan_delete_response")) {
                        log.info("价格政策删除失败");
                    }
                }
            }
            return true;
        }
        return true;
    }
}
